Component({
  data: {
    formats: {},
    bottom: 0,
    readOnly: false,
    placeholder: '开始输入...',
    _focus: false,
    editorFlag:true//在输入状态
  },
  readOnlyChange() {
    this.setData({
      readOnly: !this.data.readOnly
    })
  },
  onLoad() {
    // wx.loadFontFace({
    //   family: 'Pacifico',
    //   source: 'url("https://sungd.github.io/Pacifico.ttf")',
    //   success: console.log
    // })
    // this.onEditorReady()
  },
  
  methods: {
    
    onEditorReady() {
      const that = this;
      that.createSelectorQuery().select('#editor').context(function (res) {
        that.editorCtx = res.context;
        setTimeout(()=>{
          that.triggerEvent('sendEditorContext',res.context)
          console.log(res.context)
        },1500)
      }).exec()
      
    },
    undo() {
      this.editorCtx.undo()
    },
    redo() {
      this.editorCtx.redo()
    },
    format(e) {
      let { name, value } = e.target.dataset
      if (!name) return
      // console.log('format', name, value)
      this.editorCtx.format(name, value)
  
    },
    onStatusChange(e) {
      const formats = e.detail
      this.setData({ formats })
    },
    onEditorInput(e){
      //输入时候触发
      //获取输入的html,传给父组件
      const that=this
      // this.editorCtx.getContents({

      //   success: (res) => {        
      //     // console.log("succ：" + res.html);
      //     that.triggerEvent('sendEditorContext',res.html)
      //   },
      //   fail:(res)=>{
      //     console.log("fail：" + res); 
      //   }})
      // console.log(this.editorCtx)
      // this.triggerEvent('sendEditorContext',this.editorCtx)
    },
    insertDivider() {
      this.editorCtx.insertDivider({
        success: function () {
          console.log('insert divider success')
        }
      })
    },
    clear() {
      this.editorCtx.clear({
        success: function (res) {
          console.log("clear success")
        }
      })
    },
    removeFormat() {
      this.editorCtx.removeFormat()
    },
    insertDate() {
      const date = new Date()
      const formatDate = `${date.getFullYear()}/${date.getMonth() + 1}/${date.getDate()}`
      this.editorCtx.insertText({
        text: formatDate
      })
    },
    insertImage() {
      const that = this
      wx.chooseImage({
        count: 1,
        success: function (res) {

          const tempFilePaths = res.tempFilePaths
                    wx.uploadFile({
                    url: 'https://study.rzkeji.com/api/file/upload-image', // 仅为示例，非真实的接口地址
                    filePath: tempFilePaths[0],
                    name: 'photo',
                    header: {
                    'content-type': 'application/json' // 默认值
                    },
                    success(res) {
                        const data = JSON.parse(res.data)
                        // _this.imgList.push(data.data.img_url)
                        // _this.imgIdList.push(data.data.id)
                        that.editorCtx.insertImage({
                          src: data.data.img_url,
                          // data: {
                          //   id: 'abcd',
                          //   role: 'god'
                          // },
                          success: function () {
                            console.log('insert image success')
                          }
                        })
                    }
                    })
        }
      })
    },
    // onEditorBlur(){
    //   this.setData({editorFlag:false})

    // },
    // onEditorFocus(){
    //   this.setData({editorFlag:true})
    // }
  },
})
